package com.clinic.accessor.db;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.clinic.entity.db.Assistant;
import com.clinic.entity.db.User;
import com.clinic.entity.param.Index;

@Component
public class AssistantAccessor extends AbstractDbAccessor<Assistant> {
	
	public Assistant getAssistantById(long id) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Assistant.class);
		criteria.add(Restrictions.eq(User.Columns.USER_ID, id));
		return uniqueGeneric(criteria);
	}
	
	public List<Assistant> getAllAssistants() {
		DetachedCriteria criteria = DetachedCriteria.forClass(Assistant.class);
		criteria.add(Restrictions.gt(User.Columns.USER_ID, 0));
		return listGeneric(criteria, new Index());
	}
	
	public List<Assistant> getAllAssistants(Index index) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Assistant.class);
		criteria.add(Restrictions.gt(User.Columns.USER_ID, 0L));
		return listGeneric(criteria, new Index());
	}
	
	public List<Assistant> getOrderedAssistantsByTerm(String term, String column, Index index) {
		DetachedCriteria criteria = getCriteriaOfOrderedAssistantsByTerem(term, column);
		return listGeneric(criteria, index);
	}
	
	public int countOfOrderedAssistantsByTerm(String term, String column) {
		DetachedCriteria criteria = getCriteriaOfOrderedAssistantsByTerem(term, column);
		return getCount(criteria);
	}
	
	private DetachedCriteria getCriteriaOfOrderedAssistantsByTerem(String term, String column) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Assistant.class);
		criteria.add(Restrictions.ilike(User.Columns.NAME, "%" + term + "%"));
		criteria.addOrder(Order.asc(column));
		return criteria;
	}
	
	public List<Assistant> getAssistants(Index index) {
		DetachedCriteria criteria = getCriteriaOfAssistants();
		return listGeneric(criteria, index);
	}
	
	public int countAssistants() {
		DetachedCriteria criteria = getCriteriaOfAssistants();
		return getCount(criteria);
	}
	
	private DetachedCriteria getCriteriaOfAssistants() {
		DetachedCriteria criteria = DetachedCriteria.forClass(Assistant.class);
		return criteria;
	}
}

